package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Problem;
import com.tim.app.server.entry.Topic;
import com.tim.app.ui.activity.selected.AnswerActivity;
import com.tim.app.ui.activity.selected.TopicDetailActivity;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 问答
 */
public class QuestionAndAnswerInfoAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Problem> {

    private Context mContext;

    private int type;

    public QuestionAndAnswerInfoAdapter(Context mContext, List<Problem> mDataList, int type) {
        super(mDataList);
        this.mContext = mContext;
        this.type = type;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new InfoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_question_and_answer_info, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder mHolder, int position, final Problem data) {
        if (data == null) {
            return;
        }
        InfoViewHolder holder = (InfoViewHolder) mHolder;

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvProblemTitle.setText(data.getTitle());
        }
        if (null != data.getActionStateMsg() && null != data.getActionStateMsg().getAnswerState()) {
            holder.tvAnswer.setText(R.string.continueAnswer);
        } else {
            holder.tvAnswer.setText(R.string.goAnswer);
        }
        if (!TextUtils.isEmpty(data.getImage())) {
            holder.rivImage.setVisibility(View.VISIBLE);
            if (data.getImageUrl().size() > 0) {
                BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivImage);
            } else {
                BitmapLoader.ins().loadImage(data.getImage(), holder.rivImage);
            }
        } else {
            holder.rivImage.setVisibility(View.GONE);
        }
        holder.tvFollowCount.setText(mContext.getString(R.string.followNumber, String.valueOf(data.getConcernCount())));
        holder.tvAnswerCount.setText(mContext.getString(R.string.answerNumber, String.valueOf(data.getAnswerCount())));
        if (AppKey.PROBLEM_TYPE_RECOMMAND == type || AppKey.PROBLEM_TYPE_NEWEST == type || AppKey.PROBLEM_TYPE_HOT == type) {
            holder.rivAvatar.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(data.getTopicTitle())) {
                holder.tvSoucre.setText(mContext.getString(R.string.sourceFromSubject, data.getTopicTitle()));
            }
            holder.tvSoucre.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String topicId = data.getTopicIds();
                    Topic topic = new Topic();
                    if (topicId.contains(",")) {
                        topic.setId(topicId.split(",")[0]);
                    } else {
                        topic.setId(topicId);
                    }
                    TopicDetailActivity.start(mContext, topic, false);
                }
            });
        } else if (AppKey.PROBLEM_TYPE_BE_INVIED == type) {
            holder.rivAvatar.setVisibility(View.VISIBLE);
            if (null != data.getUserMsg()) {
                if (!TextUtils.isEmpty(data.getUserMsg().getImage())) {
                    BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
                }
                if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                    holder.tvSoucre.setText(mContext.getString(R.string.fromSomeoneInvited, data.getUserMsg().getName()));
                }
            }
        }
        holder.tvAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentAnswer = new Intent(mContext, AnswerActivity.class);
                intentAnswer.putExtra(AppKey.IS_ANSWER, true);
                intentAnswer.putExtra(AppKey.QUESTION_ID, data.getId());
                mContext.startActivity(intentAnswer);
            }
        });
    }

    public class InfoViewHolder extends BaseRecyclerViewHolder {

        RatioImageView rivImage;
        LinearLayout llSoure;
        RoundedImageView rivAvatar;
        TextView tvSoucre, tvProblemTitle, tvAnswer, tvFollowCount, tvAnswerCount;

        public InfoViewHolder(View itemView) {
            super(itemView);
            rivImage = (RatioImageView) itemView.findViewById(R.id.rivImage);
            llSoure = (LinearLayout) itemView.findViewById(R.id.llSoure);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvSoucre = (TextView) itemView.findViewById(R.id.tvSoucre);
            tvProblemTitle = (TextView) itemView.findViewById(R.id.tvProblemTitle);
            tvAnswer = (TextView) itemView.findViewById(R.id.tvAnswer);
            tvFollowCount = (TextView) itemView.findViewById(R.id.tvFollowCount);
            tvAnswerCount = (TextView) itemView.findViewById(R.id.tvAnswerCount);
        }

    }

}
